package com.example.service.impl;

import com.example.dao.UserMapper;
import com.example.dto.UserReq;
import com.example.model.User;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    public int save(User user){
        return mapper.insert(user);
    }

    public int update(User user){
        return mapper.updateByPrimaryKey(user);
    }

    public int delete(Integer userId){
        return mapper.deleteByPrimaryKey(userId);
    }

    public User getUserById(Integer userId){
        return mapper.selectByPrimaryKey(userId);
    }

    @Override
    public User getUserByUsernameAndPasswd(String userName, String password) {
        User user = null;
        try{
            user = mapper.getUserByUsernameAndPasswd(userName, password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }


    public PageInfo findAll(UserReq userReq,int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);

        List<User> list = mapper.selectAllUser(userReq);
        PageInfo result = new PageInfo(list);
        return result;
    }
}
